﻿using GameEditor;
using GameEditor.View.Components;
using GameEngine.Model;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using System.Windows.Controls;
using System;
using GameEngine.Model.Data;
using GameEngine;
using GameEditor.View;

namespace GameEditor.Commands
{
     public class Page_WorldPPropertiesEditor_MenuAction_Command<MyIdType> : SimpleCommand
         where MyIdType : IComparable<MyIdType>
    {
        override public void Execute(INotification notification)
        {
            bool exit = false;
            var mediator = Facade.RetrieveMediator(GameEditorFacade.WorldEditorMediatorName) as WorldPropertiesEditor_MenuMediator<MyIdType>;
            switch ((IWorldEditorMenuViewConstants.ButtonTags)notification.Body)
            {
                //case IWorldEditorMenuViewConstants.ButtonTags.CANCEL:
                //    SendNotification(MyFacade.MY_PAGE_LEVELEDITOR_IN_COMMAND);
                //    exit = true;
                //    break;
                //case IWorldEditorMenuViewConstants.ButtonTags.OK:
                //    //mediator to proxy, to file
                //    SendNotification(MyFacade.MY_PAGE_LEVELEDITOR_IN_COMMAND);
                //    exit = true;
                //    break;
                case IWorldEditorMenuViewConstants.ButtonTags.NEWTHING:
                    {
                        //TODO devia aparecer uma janela nova com as opcoes

                        var thingType = mediator.SelectedThingType;
                        var thingName = mediator.NewThingName;

                        var vo = ModelFacade<MyIdType>.Instance.CreateThingVO(
                            new ThingVOId<MyIdType>(ModelFacade<MyIdType>.Instance.CreateNewId()) { Name = thingName },
                            mediator.SelectedThingType);
                        var worldDataProxy = ModelFacade<MyIdType>.Helper.GetWorldDataProxy(mediator.WorldId);
                        ApplicationFacadeHelper<MyIdType>.AddThingToWorld(
                            vo,
                            worldDataProxy.VO.Id);
                        ApplicationFacadeHelper<MyIdType>.ControlThingPosition(vo.Id);
                        //ApplicationFacadeHelper<int>.SelectThing(vo.Id, worldDataProxy.Id);
                        
                        //SilverlightViewFacade<EditorGameUC,EditorLevelUC>.Helper.AddThingToWorld(
                        //    vo,
                        //    SilverlightModelFacade.Helper.GetWorldDataProxy(mediator.WorldId).VO);
                        
                        //var worldMed = SilverlightViewFacade<EditorGameUC,EditorLevelUC>.Helper.GetWorldMediator(mediator.WorldId);
                        //var mapMed = worldMed.mapMediator;

                        
                        //mediator ok/cancel childwindow ou new ecra
                        //var creator = creatorMed.ViewComponent;
                        //ChildWindow childWnd = new ChildWindow()
                        //{
                        //    Content = creator
                        //};
                        //creator.OnCreateItemClick += new Action<string>(delegate(string arg1)
                        //{
                        //    childWnd.Close();
                        //});
                        //childWnd.Show();
                        //SendNotification(
                        //itemListBox.SelectionChanged += new SelectionChangedEventHandler(itemListBox_SelectionChanged);

                    }
                    exit = true;
                    break;
                //case IWorldEditorViewConstants.ButtonTags.EDITTHING:
                //    {
                //        var dataProxy = MyModelFacade.Helper.GetLevelDataProxy(mediator.Selected);
                //        SendNotification(MyFacade.MY_PAGE_THINGEDITOR_IN_COMMAND, dataProxy);
                //    }
                //    exit = true;
                //    break;
                case IWorldEditorMenuViewConstants.ButtonTags.REMOVETHING:
                    {
                        var thing = mediator.SelectedThing;

                        ApplicationFacadeHelper<MyIdType>.RemoveThingFromWorld(thing, mediator.WorldId);
                    }
                    break;
                case IWorldEditorMenuViewConstants.ButtonTags.COPYASNEWTHING:
                    //SendNotification(MyFacade.PAGE_EDITORLOADGAME_IN_COMMAND);
                    break;
            }
            if (exit)
                Facade.RemoveMediator(GameEditorFacade.LevelEditorMediatorName);
        }
    }
}
